﻿#pragma once
#include <QSqlDataBase>
#include <QVariantMap>
#include <QSqlQuery>
#include "ADFGlobal.h"

class ADF_EXPORT AsDbOperator
{
public:
	AsDbOperator();
	virtual ~AsDbOperator();

public:
	virtual bool openDB() = 0;
	virtual QVariant operInsert( QString tblName, const QVariantMap &vm, QString primaryKey, QString connName="") = 0;
	virtual bool operDelete(QString tblName, QString conditionKey, QVariant conditionValue, QString connName = "") = 0;
	virtual bool operUpdate(QString tblName, QVariantMap &vm, QString primaryKey, QString connName = "") = 0;
	virtual QSqlQuery operQuery(QString query, QString connName = "") = 0;
	virtual bool execute(QString sql, QString connName = "") = 0;

protected:
	virtual QSqlDatabase onGetOpenDb(QString connectionName) = 0;

public:
	inline void setIp(QString ip) { m_ip = ip; }
	inline void setPort(int port) { m_port = port; }
	inline void setUsername(QString username) { m_username = username; }
	inline void setPassword(QString pwd) { m_pwd = pwd; }
	inline void setDatabaseName(QString dbName) { m_dbName = dbName; }

	inline void setDbConnInfo(QString ip, int port, QString userName, QString pwd, QString dbName) 
	{
		m_ip = ip;
		m_port = port;
		m_username = userName;
		m_pwd = pwd;
		m_dbName = dbName;
	}

private:
	QString m_connectionName;		//连接名
protected:
	inline QString getConnectionName() { return m_connectionName; }

protected:
	//数据库连接信息
	QString m_ip;
	int m_port;
	QString m_username;
	QString m_pwd;
	QString m_dbName;

	QString m_dbType;
private:
	static int ms_dbIndex;
};
